;
(function () {

  'use strict';

  angular.module('services.customActivity.summary')
    .service('summaryCheckingFileService', summaryCheckingFileService)
    .service('summaryCheckingDiffService', summaryCheckingDiffService);

  function summaryCheckingFileService(Restangular, generatorSummaryPageService, COREDATA) {
    var that = this;
    var option = {
      title: {
        text: '',
        x: 'center'
      },
      tooltip: {
        trigger: 'axis',
        formatter: "{b}: {c}"
      },
      legend: {
        data: []
      },
      toolbox: {},
      xAxis: [
        {
          type: 'category',
          data: []
        }
      ],
      yAxis: [
        {
          type: 'value',
          name: '未按时到达次数',
          axisLabel: {show: true, interval: 0, textStyle: {baseline: "middle"}}
        }
      ],
      series: [
        {
          type: 'bar',
          data: []
        }
      ],
      grid: {
        y: '20%',
        height: '62%'
      }
    };

    this.queryBySys = function (busLine, element) {

      var title = '';
      var channel = '';

      switch (busLine) {
        case 'UPAY':
          channel = '';
          title = '统一支付';
          break;
        case 'TMALL':
          channel = '1';
          title = '天猫旗舰店';
          break;
        case 'SCPAY':
          channel = '3';
          title = '移动商城';
          break;
        case 'BANK':
          channel = '2';
          title = '银行总对总';
          break;
        case 'VCES':
          channel = '5';
          title = '有价卡';
          break;
        case 'LINGXI':
          channel = '4';
          title = '灵犀语音';
          break;
      }

      if (generatorSummaryPageService.pageInfo.preview) {
        setTimeout(function () {
          option.series[0].data = [];
          option.xAxis[0].data = [];
          option.title.text = title;
          that.loading = true;
          angular.forEach(generatorSummaryPageService.pageInfo.provinceCodes, function (code) {
            angular.forEach(COREDATA.PROVINCES, function (province) {
              if (code === province.provinceCode) {
                option.series[0].data.push(0);
                option.xAxis[0].data.push(province.provinceName);
              }
            });
          });
          (echarts.init(element[0])).setOption(option);
        }, 0);
        return;
      }

      that.loading = false;
      Restangular.all('sas/OMdisplay/reconciliationsAbnormal/fileUnreceivedStatistic').post({
        channelCode: channel,
        endTime: generatorSummaryPageService.pageInfo.endTime,
        provinceCode: generatorSummaryPageService.pageInfo.summaryProvinceCode,
        provincial: "1",
        startTime: generatorSummaryPageService.pageInfo.startTime,
        timeType: "1"
      }).then(function (result) {
        that.loading = true;
        option.title.text = title;
        option.series[0].data = [];
        option.xAxis[0].data = [];
        if (result.state === '10000') {
          angular.forEach(result.data, function (obj) {
            option.series[0].data.push(obj.unreceivedCount);
            option.xAxis[0].data.push(obj.provinceName);
          });
        }
        (echarts.init(element[0])).setOption(option);
      });
    }
  }

  summaryCheckingFileService.$inject = ['Restangular', 'generatorSummaryPageService', 'COREDATA'];

  function summaryCheckingDiffService(Restangular, generatorSummaryPageService, COREDATA) {

    var that = this;

    var option = {
      title: {
        text: '',
        x: 'center'
      },
      tooltip: {
        trigger: 'axis',
        formatter: "{b}: {c}%"
      },
      legend: {
        data: []
      },
      toolbox: {},
      xAxis: [
        {
          type: 'category',
          data: []
        }
      ],
      yAxis: [
        {
          name: '差异率',
          type: 'value',
          axisLabel: {formatter: '{value}%', show: true, interval: 0, textStyle: {baseline: "middle"}}
        }
      ],
      series: [
        {
          type: 'bar',
          data: []
        }
      ],
      grid: {
        y: '20%',
        height: '62%'
      }
    };

    this.queryBySys = function (busLine, element) {

      var title = '';
      var channel = '';

      switch (busLine) {
        case 'UPAY':
          channel = '';
          title = '统一支付';
          break;
        case 'TMALL':
          channel = '1';
          title = '天猫旗舰店';
          break;
        case 'SCPAY':
          channel = '3';
          title = '移动商城';
          break;
        case 'BANK':
          channel = '2';
          title = '银行总对总';
          break;
        case 'VCES':
          channel = '5';
          title = '有价卡';
          break;
        case 'LINGXI':
          channel = '4';
          title = '灵犀语音';
          break;
      }

      if (generatorSummaryPageService.pageInfo.preview) {
        setTimeout(function () {
          option.series[0].data = [];
          option.xAxis[0].data = [];
          option.title.text = title;
          that.loading = true;
          angular.forEach(generatorSummaryPageService.pageInfo.provinceCodes, function (code) {
            angular.forEach(COREDATA.PROVINCES, function (province) {
              if (code === province.provinceCode) {
                option.series[0].data.push(0.00);
                option.xAxis[0].data.push(province.provinceName);
              }
            });
          });
          (echarts.init(element[0])).setOption(option);
        }, 0);
        return;
      }

      that.loading = false;
      Restangular.all('sas/OMdisplay/reconciliationsAbnormal/differencesCount').post({
        channelCode: channel,
        endTime: generatorSummaryPageService.pageInfo.endTime,
        provinceCode: generatorSummaryPageService.pageInfo.summaryProvinceCode,
        provincial: "1",
        startTime: generatorSummaryPageService.pageInfo.startTime,
        timeType: "1"
      }).then(function (result) {
        that.loading = true;
        if (result.state === '10000') {
          option.title.text = title;
          option.series[0].data = [];
          option.xAxis[0].data = [];
          angular.forEach(generatorSummaryPageService.pageInfo.provinceCodes, function (code) {
            var differencesRate = 0;
            var provinceName = '';
            angular.forEach(result.data, function (obj) {
              if (code === obj.provinceCode) {
                //如果是统一支付就累加三条业务线的成功率
                if (busLine === 'UPAY') {
                  differencesRate += obj.differencesRate;
                } else {
                  differencesRate = obj.differencesRate;
                }
                provinceName = obj.provinceName;
              }
            });
            option.series[0].data.push(differencesRate.toFixed(2));
            option.xAxis[0].data.push(provinceName);
          });
          (echarts.init(element[0])).setOption(option);
        }
      });
    }
  }

  summaryCheckingDiffService.$inject = ['Restangular', 'generatorSummaryPageService', 'COREDATA'];
})();